package com.pj.point;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.snackbar.Snackbar;

public class Login extends AppCompatActivity implements View.OnClickListener {
    EditText account;
    EditText password;
    Button login;
    TextView register;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ActionBar bar = getSupportActionBar();
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setTitle("登录");
        account = findViewById(R.id.account);
        password = findViewById(R.id.password);
        login = findViewById(R.id.login);
        register = findViewById(R.id.register);
        login.setOnClickListener(this);
        register.setOnClickListener(this);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        finish();
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId())
        {
            case R.id.login:{
                String act = account.getText().toString();
                String pwd = password.getText().toString();
                if (act.equals("")||pwd.equals(""))
                {
                    Snackbar.make(v,"您犯了一个非常愚蠢的错误，让我们来看看它是什么",Snackbar.LENGTH_SHORT)
                            .setAction("查看", new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    Toast.makeText(getApplicationContext(), "请填写用户名和密码", Toast.LENGTH_LONG).show();
                                }
                            }).show();
                }
                else
                {
                    SharedPreferences user = getSharedPreferences("data", MODE_PRIVATE);
                    if (user.contains("username"))
                    {
                        String username = user.getString("username", "");
                        String password = user.getString("password", "");
                        if(!act.equals(username))
                        {
                            Toast.makeText(this, "账号名不存在", Toast.LENGTH_SHORT).show();
                        }
                        else if (!pwd.equals(password))
                        {
                            Toast.makeText(this, "密码错误", Toast.LENGTH_SHORT).show();
                        }
                        else
                        {
                            Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
                            SharedPreferences.Editor edit = user.edit();
                            edit.putBoolean("isLogin",true);
                            edit.apply();
                            finish();
                        }
                    }
                    else
                    {
                        Snackbar.make(v,"系统检测到你貌似都没注册过账号，要不先去注册一个？",Snackbar.LENGTH_SHORT)
                                .setAction("去注册", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        Intent intent = new Intent(Login.this, Register.class);
                                        startActivity(intent);
                                    }
                                }).show();
                    }
                }
                break;
            }
            case R.id.register:{
                Intent intent = new Intent(Login.this, Register.class);
                startActivity(intent);
                break;
            }
        }
    }
}